package short_semester.questionnaire_planet.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import short_semester.questionnaire_planet.dao.QuestionnaireDao;
import short_semester.questionnaire_planet.dao.UserDao;
import short_semester.questionnaire_planet.entity.User;
import short_semester.questionnaire_planet.pojo.Questionnaire;
import short_semester.questionnaire_planet.request.editRequest.*;
import short_semester.questionnaire_planet.response.CommonResponse;
import short_semester.questionnaire_planet.service.editService.*;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RestController
@RequestMapping("api/edit")
public class EditController {
    
    @Autowired
    QuestionnaireService questionnaireService;
    
    @Autowired
    QuestionnaireDao questionnaireDao;
    
    @Autowired
    QuestionService questionService;
    
    @Autowired
    SingleChoiceQuestionService singleChoiceQuestionService;
    
    @Autowired
    MultiChoiceQuestionService multiChoiceQuestionService;
    
    @Autowired
    FillBlankQuestionService fillBlankQuestionService;
    
    @Autowired
    ScoreQuestionService scoreQuestionService;
    
    @Autowired
    UserDao userDao;
    
    @PostMapping("getQuestionnaire")

    public CommonResponse getQuestionnaire(@RequestParam(value = "id", required = false)String id,
                                          @RequestParam("questionnaireId")Integer questionnaireId)
    {
        if(id==null||id.isEmpty()){
            CommonResponse response = new CommonResponse();
            response.setCode(-1);
            response.setMsg("当前用户无编辑此问卷的权限");
            return response;
        }
        
        Optional<User> optional = userDao.findById(Integer.parseInt(id));
        if(optional.isPresent()){
            User user = optional.get();
            Questionnaire questionnaire = questionnaireDao.getById(questionnaireId);
            if(!Objects.equals(questionnaire.getUser().getId(), user.getId())){
                CommonResponse response = new CommonResponse();
                response.setCode(-1);
                response.setMsg("当前用户无编辑此问卷的权限");
                return response;
            }
        }else{
            CommonResponse response = new CommonResponse();
            response.error("用户不存在");
            return response;
        }
        return questionnaireService.getQuestionnaire(questionnaireId);
    }
    
    @PostMapping("editQuestionnaire")
    public CommonResponse editQuestionnaire(@RequestBody EditQuestionnaireBody body)
    {
        CommonResponse response = new CommonResponse();
        
        try{
            Questionnaire questionnaire =null;
            Optional<Questionnaire> optional = questionnaireDao.findById(body.getQuestionnaireId());
            if(optional.isPresent()){
                questionnaire = optional.get();
            }else{
                response.error("问卷"+body.getQuestionnaireId()+"不存在");
                return response;
            }

            questionnaire.setValueFrom(body);

            questionnaireDao.save(questionnaire);
            response.setMsg("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            response.error(e.getMessage());
        }
        
        
        return response;
    }
    
    @PostMapping("addQuestion")
    public CommonResponse addQuestion(@RequestParam Integer questionnaireId,
                                      @RequestParam String type,
                                      @RequestParam(required = false) String qtype){
        Optional<Questionnaire> optional = questionnaireDao.findById(questionnaireId);
        Questionnaire questionnaire =null;
        if(optional.isPresent()){
            questionnaire = optional.get();
        }else{
            CommonResponse response = new CommonResponse();
            response.error("问卷"+questionnaireId+"不存在");
            return response;
        }

        return questionService.addQuestion(questionnaire,type,qtype);
    }
    
    @PostMapping("deleteQuestion")
    public CommonResponse deleteQuestion(@RequestParam Integer questionId,@RequestParam String type){
        return questionService.deleteQuestion(questionId,type);
    }
    
    @PostMapping("changeOrder")
    public CommonResponse changeOrder(@RequestBody ChangeOrderBody body){
        return questionService.changeOrder(body.getQuestionIds());
    }
    
    @PostMapping("editRadio")
    public CommonResponse editRadio(@RequestBody EditQuestionBody body){
        return singleChoiceQuestionService.editQuestion(body);
    }
    
    @PostMapping("editCheckBox")
    public CommonResponse editCheckBox(@RequestBody EditQuestionBody body){
//        System.out.println(body);
        return multiChoiceQuestionService.editQuestion(body);
    }
    
    @PostMapping("editScore")
    public CommonResponse editScore(@RequestBody EditScoreQuestionBody body){
        return scoreQuestionService.editQuestion(body);
    }
    
    @PostMapping("editCompletion")
    public CommonResponse editCompletion(@RequestBody EditFillBlankQuestionBody body){
        return fillBlankQuestionService.editQuestion(body);
    }
    
    @PostMapping("getRadio")
    public CommonResponse getRadio(@RequestParam Integer questionId){
        return singleChoiceQuestionService.getQuestionResponse(questionId);
    }
    
    @PostMapping("getCheckBox")
    public CommonResponse getCheckBox(@RequestParam Integer questionId){
        return multiChoiceQuestionService.getQuestionResponse(questionId);
    }
    
    @PostMapping("getCompletion")
    public CommonResponse getCompletion(@RequestParam Integer questionId){
        return fillBlankQuestionService.getQuestionResponse(questionId);
    }
    
    @PostMapping("getScore")
    public CommonResponse getScore(@RequestParam Integer questionId){
        return scoreQuestionService.getQuestionResponse(questionId);
    }
}
